package com.example.programme.home;


import static com.example.programme.database.UserDBHelper.TB_NAME;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;


import android.app.Activity;
import android.app.ActivityOptions;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.programme.LoginActivity;
import com.example.programme.MyToast;
import com.example.programme.R;
import com.example.programme.database.UserDBHelper;

public class SetAccountAcivity extends AppCompatActivity {
    TextView text_name;
    EditText edit_password;
    Button resetpassword;
   // @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_account);

        SharedPreferences sharedPreferences = getSharedPreferences("User_Account",
                Activity.MODE_PRIVATE);
        String name=sharedPreferences.getString("name",null);
        String password=sharedPreferences.getString("password",null);
        text_name=findViewById(R.id.text_name);
        edit_password=findViewById(R.id.edit_password);
        text_name.setText(name);
        edit_password.setText(password);

        resetpassword=findViewById(R.id.resetpassword);
        resetpassword.setOnClickListener(new View.OnClickListener() {
           // @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {
               String newpassword=edit_password.getText().toString().trim();
               if(newpassword.equals(password))
                   MyToast.makeText(SetAccountAcivity.this,"未修改密码",Toast.LENGTH_LONG);
                else { UserDBHelper userHelper = new UserDBHelper(SetAccountAcivity.this);
                   SQLiteDatabase db = userHelper.getWritableDatabase();
                   ContentValues values = new ContentValues();
                   values.put("Password",newpassword);
                   db.update(TB_NAME,values,"Name=?",new String[]{name});


                   SharedPreferences sp= getSharedPreferences("User_Account",Activity.MODE_PRIVATE);
                   SharedPreferences.Editor editor = sp.edit();
                   editor.putString("password", newpassword);
                   editor.apply();

                   MyToast.makeText(SetAccountAcivity.this,"修改成功!",Toast.LENGTH_LONG);
                   Intent intent = new Intent(SetAccountAcivity.this, LoginActivity.class);
                    //startActivity(intent);
                   startActivity(intent,
                           ActivityOptions.makeSceneTransitionAnimation(SetAccountAcivity.this).toBundle());
                   supportFinishAfterTransition();

                   new HomeActivity().instance.finish();//修改密码成功，返回登录页面，且关闭HomeActivity

                }

            }
        });



    }
}